<template>
	<view class="auth-check-component">
		<Tips title="小贴士" :des="des" />
		<view class="main">
			<view v-if="hasContent">
				<list>
					<view  v-for="(item, index) in permissionList.permissions" :key="index"  class="customCell list-item">
						<view class="van-cell-text" @click="() =>jumpPage(item)">{{ item.permissionName }}</view>
						<view @click="() =>jumpPage(item)" class="van-cell-text postion-about" :style="{'color': queryStatus(item.status).color }">{{ queryStatus(item.status).name }}</view>
					</view>
				</list>
			</view>
			<view v-else><Empt description="暂无数据"/></view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import {ref, onMounted, computed} from 'vue'
	import Tips from "@/components/tips/index.vue"
	import Empt from "@/components/Empt/index.vue"
	import { homeServer, vipServer } from '@/common/index'
	import {useStudentListStore} from '@/stores/useStudentList.js'
	const studentStore = useStudentListStore();
	const curStud = computed(() => studentStore.currentStudent || {});
	const description = ref("暂无数据")
	// 获取家长信息
	const getMemberInfo = async () => {
		try{
			const { status, data } = await homeServer.getMemberInfo()
			if(status === '200'){
				const { mobileBrand, systemVersion } = data
				const params = {
					studentId: curStud.value?.id,
					studentMobileBrand: mobileBrand,
					studentSystemVersion: systemVersion
				}
				getMobilePermission(params)
			}
		}catch(e){
			console.log(e)
			//TODO handle the exception
		}
	}
	const queryStatus = (status) => {
		// 1： 开始 -1： 未开启
		const statusList = [
			{
				name: '开启',
				value: 1,
				color:"#2BC888"
			},
			{
				name: '未开启',
				value: -1,
				color:"#FF6363"
			},
			{
				name: '查看',
				value: 3,
				color:"#ccc"
			}
		];
		return  statusList.find(item => item.value === Number(status))
	}
	const des = ref([
			"温馨提示：权限未开启将影响软件功能",
			"注意事项：1.退出省电模式 2.关闭“健康使用手机",
		])
	const permissionList = ref([]); // 权限自检列表
	const hasContent = ref(false); // true： 有权限数据 false： 没有权限数据
	 // permissionResultType: -1: 不适配 1: 权限未开启 2:  权限部分开启 3: 权限所有开启
	 // permissions： {permissionName, status, stepNo} 
	const getMobilePermission = async () => {
		if(!curStud.value?.id){
			console.log("暂无学生，请绑定学生")
			hasContent.value = false
			return
		}
		const params = {
			channel:"xcx",
			studentId: curStud.value?.id,
			studentMobileBrand: curStud.value?.mobileBrand,
		   studentSystemVersion: curStud.value?.systemVersion
		}
		try{	
			// api  http://139.9.121.96:8281/stu-mob-mon-customer/doc.html#/default/%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF%E7%9B%B8%E5%85%B3%E6%8E%A5%E5%8F%A3/mobilePermissionCheckUsingGET
			const { status, data } = await vipServer.getMobilePermission(params)
			if(status === '200'){
				permissionList.value = data;
				const isShow = () => data.permissionResultType !== -1 && data.permissions.length > 0; // 不适配
				hasContent.value = isShow()
			}
			
		}catch(e){
			//TODO handle the exception
		}
	}
	const jumpPage = (item) => {
		const [url, options] = item.url.split("?");
		let r = `/pagesD/authwebview/index?url=${url}`
		console.log('r', r)
		uni.setStorageSync(`${url}`, options)
		uni.navigateTo({
			url:r
		})
	}
	onMounted(() => {
		getMobilePermission()
	})
</script>

<style scoped lang="scss">
	.auth-check-component{
		.main{
			margin-top: 40upx;
			.inline{
				    display: flex;
				    justify-content: space-between;
				    align-items: center
			}
		}
	}
	
	
	.list-item{
		position: relative;
		.postion-about{
			position: absolute;
			right: 30upx;
			top: 37%;
		}
	}
</style>
